package com.xiaochong.tir.apiservice.component;

import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.apiservice.component.hbase.CurrencyInfoHbaseComponent;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.apiservice.util.SpringContextUtil;
import com.xiaochong.tir.common.data.hbasepo.*;
import com.xiaochong.tir.common.util.*;
import org.apache.commons.collections.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wujiaxing on 2018/5/7.
 */
public class AkkaDiagnosisScore extends UntypedActor {


    private CmcDataCalculateComponent cmcDataCalculateComponent =
            (CmcDataCalculateComponent) SpringContextUtil.getBean("cmcDataCalculateComponent");

    private CoinMarketCapDataComponent coinMarketCapDataComponent =
            (CoinMarketCapDataComponent) SpringContextUtil.getBean("coinMarketCapDataComponent");

    private CurrencyInfoHbaseComponent currencyInfoHbaseComponent =
            (CurrencyInfoHbaseComponent) SpringContextUtil.getBean("currencyInfoHbaseComponent");

    private RedisServer redisServer =
            (RedisServer) SpringContextUtil.getBean("redisServer");

    @Override
    public void onReceive(Object o) {
        LogTrace.beginTrace("货币诊断得分计算");
        Object o1 = ((Object[]) o)[0];
        Map<String,Object> map = (Map<String, Object>) o1;

        List<CmcCurrencyInfo> cmcCurrencyInfos =
                (List<CmcCurrencyInfo>) map.get(AkkaParamUtils.CMC_CURRENCY_INFO_LIST);
        List<DailyComprehensiveAnalysisInfo> dailyComprehensiveAnalysisInfos =
                (List<DailyComprehensiveAnalysisInfo>) map.get(AkkaParamUtils.DAILY_COMPREHENSIVE_ANALYSIS_INFO_LIST);
        CmcCurrencyGlobalInfo cmcCurrencyGlobalInfo =
                (CmcCurrencyGlobalInfo) map.get(AkkaParamUtils.CMC_GLOBAL_INFO);
        Map<String, DailyComprehensiveAnalysisInfo> dailyInfoMap =
                (Map<String, DailyComprehensiveAnalysisInfo>) map.get(AkkaParamUtils.DAILY_COMPREHENSIVE_ANALYSIS_INFO_MAP);

        LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_SCORE_TITLE, LogTraceKeysUtils.DATA,"货币诊断得分相关计算开始");

        LocalDateTime dateTime = DateUtils.dateToLocalDateTime(cmcCurrencyInfos.get(0).getCreateTime());

        String flag =
                redisServer.get(RedisKeysUtils.GOOGLE_TRENDS_FLAG
                        + DateUtils.localDateTimeFormatString(dateTime, DateUtils.format_ymdh));
        List<TrendsInfo> googleTrends = null;
        if(!"true".equals(flag)){

            googleTrends = coinMarketCapDataComponent.getGoogleTrends(cmcCurrencyInfos.get(0).getCreateTime());
            LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_SCORE_TITLE, LogTraceKeysUtils.DATA, "诊断详情-市场热度计算完成");
            LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_SCORE_TITLE, LogTraceKeysUtils.DATA, JSON.toJSONString(googleTrends));
            redisServer.set(RedisKeysUtils.GOOGLE_TRENDS_FLAG
                    + DateUtils.localDateTimeFormatString(dateTime, DateUtils.format_ymdh),"true");
        }
        if(CollectionUtil.isBlank(googleTrends)){
            googleTrends = currencyInfoHbaseComponent.getLaseGoogleTrends();
        }



        List<DiagnosisQuantumEnergy> diagnosisQuantumEnergies = cmcDataCalculateComponent.calculateDiagnosisQuantumEnergy(cmcCurrencyInfos);
        LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_SCORE_TITLE, LogTraceKeysUtils.DATA, "诊断详情-量能变化计算完成");
        if(diagnosisQuantumEnergies!=null && diagnosisQuantumEnergies.size()>0){
            currencyInfoHbaseComponent.saveDiagnosisQuantumEnergy(diagnosisQuantumEnergies);
        }else{
            diagnosisQuantumEnergies = currencyInfoHbaseComponent.getLastDiagnosisQuantumEnergy();
        }
        List<Liquidity> liquidity = null;
        try {
            liquidity = cmcDataCalculateComponent.liquidity(dailyInfoMap, cmcCurrencyGlobalInfo);
            LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_SCORE_TITLE, LogTraceKeysUtils.DATA, "货币流动性计算完成");
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("货币流动性计算完成异常" , e);
        }

        if(liquidity!=null && liquidity.size()>0){
            currencyInfoHbaseComponent.saveLiquidity(liquidity);
        }else{
            liquidity = currencyInfoHbaseComponent.getLastLiquidity();
        }

        Map<String, PopularityIndicator> popularityIndicatorMap = currencyInfoHbaseComponent.getLastPopularityIndicator();
        if(CollectionUtil.isNotBlank(popularityIndicatorMap)){
            Date popularityIndicatorDate = null;
            for (String key:popularityIndicatorMap.keySet()) {
                if(popularityIndicatorMap.get(key)!=null && popularityIndicatorDate==null){
                    popularityIndicatorDate = popularityIndicatorMap.get(key).getCreateTime();
                }else{
                    break;
                }
            }
            if(popularityIndicatorDate!=null && dateTime.getHour()!=DateUtils.dateToLocalDateTime(popularityIndicatorDate).getHour()){
                popularityIndicatorMap = cmcDataCalculateComponent.calculatePopularityIndicatorNew();
                if(CollectionUtil.isNotBlank(popularityIndicatorMap)){
                    currencyInfoHbaseComponent.savePopularityIndicator(popularityIndicatorMap);
                }
            }
        }else{
            popularityIndicatorMap = cmcDataCalculateComponent.calculatePopularityIndicatorNew();
            if(CollectionUtil.isNotBlank(popularityIndicatorMap)){
                currencyInfoHbaseComponent.savePopularityIndicator(popularityIndicatorMap);
            }
        }
        LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_SCORE_TITLE, LogTraceKeysUtils.DATA, "诊断详情-人气意愿指标性计算完成");

        List<DiagnosisScore> diagnosisScores =
                cmcDataCalculateComponent.calculateDiagnosisScore(googleTrends,diagnosisQuantumEnergies, liquidity, popularityIndicatorMap);
        LogTrace.info(LogTraceKeysUtils.AKKA_DIAGNOSIS_SCORE_TITLE, LogTraceKeysUtils.DATA, "货币诊断得分计算完成");
        if(CollectionUtils.isNotEmpty(diagnosisScores)){
            cmcDataCalculateComponent.saveDiagnosisScores(diagnosisScores);
        }
        LogTrace.endTrace();
    }
}
